package cn.engage.product.service.impl;

import cn.engage.basic.util.BaiduAuditUtils;
import cn.engage.pet.domain.Pet;
import cn.engage.pet.domain.PetOnlineAuditLog;
import cn.engage.product.domain.Product;
import cn.engage.product.domain.ProductOnlineAuditLog;
import cn.engage.product.mapper.ProductDetailMapper;
import cn.engage.product.mapper.ProductMapper;
import cn.engage.product.mapper.ProductOnlineAuditLogMapper;
import cn.engage.product.service.IProductService;
import cn.engage.basic.service.impl.BaseServiceImpl;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author engage
 * @since 2022-11-03
 */
@Service
public class ProductServiceImpl extends BaseServiceImpl<Product> implements IProductService {
    @Autowired
    private ProductMapper productMapper;

    @Autowired
    private ProductDetailServiceImpl productDetailService;

    @Autowired
    private ProductOnlineAuditLogMapper productOnlineAuditLogMapper;

    @Override
    public void add(Product product) {
        product.setSalecount(0l);
        product.setState(0);
        super.add(product);
        product.getDetail().setProductId(product.getId());
        productDetailService.add(product.getDetail());
    }
    /* 产品下架*/
    @Override
    public void offsale(List<Long> ids) {
        productMapper.batchOffsale(ids);
    }
    /* 产品上架 */
    @Override
    public void onsale(List<Long> ids) {
        // 筛选出下架状态的商品上架
        Map<Long, Product> map = productMapper.findAll().stream().filter(product -> product.getState() == 0).collect(Collectors.toMap(Product::getId, e -> e));
        // 遍历id去map里去对应信息
        StringBuilder stringbuilder = new StringBuilder();
        Date date = new Date();
        for (Long id : ids) {
            Product product = map.get(id);
            // 不为空则表示要上架，进行参数校验
            if (product != null){
                if (!BaiduAuditUtils.TextCensor(product.getName())){
                    stringbuilder.append("产品上架百度AI审核名称失败，产品名称为：").append(product.getName()).append(";");
                }
                if (product.getResources().split(",").length > 1){
                    // 有多张图片，图片用逗号隔开的，循环遍历
                    for (String s : product.getResources().split(",")) {
                        if (!BaiduAuditUtils.ImgCensor(s)){
                            stringbuilder.append("产品上架百度AI审核图片失败，产品图片地址为：").append(product.getResources()).append(";");
                        }
                    }
                }else {
                        if (!BaiduAuditUtils.ImgCensor(product.getResources())){
                            stringbuilder.append("产品上架百度AI审核图片失败，产品图片地址为：").append(product.getResources()).append(";");
                        }
                }
                // 表示有图片或者名字不合格
                if (stringbuilder.length() > 0){
                    ProductOnlineAuditLog petOnlineAuditLog = ProductOnlineAuditLog.builder()
                            .auditTime(date)
                            .productId(id)
                            .state(0)
                            .note(stringbuilder.toString())
                            .build();
                    productOnlineAuditLogMapper.add(petOnlineAuditLog);
                    continue;
                }
                product.setState(1);
                product.setOnsaletime(date);
                productMapper.update(product);
            }
        }
    }

    @Override
    public Product findProdectDetail(Long id) {
        return productMapper.findProdectDetail(id);
    }
}
